/** 
 * @Title:curator管理类 ,新增、修改、删除、读取 
 * @Desription:curator管理类,新增、修改、删除、读取
 * @Company:CSN
 * @ClassName:CuratorManager.java
 * @Author:Justic
 * @CreateDate:2015-6-30   
 * @UpdateUser:Justic  
 * @Version:0.1 
 *    
 */

package com.justic.zk.quartz.curator;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.GetDataBuilder;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.data.Stat;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

/**
 * @ClassName: CuratorManager
 * @Description: curator管理类,新增、修改、删除、读取
 * @author: Justic
 * @date: 2015-6-30
 * 
 */
public class CuratorManager {

	/**
	 * create Node
	 * 
	 * @param curator
	 * @param nodeName
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public boolean createNode(CuratorFramework curator, String nodeName, String value) throws Exception {

		boolean suc = false;

		Stat stat = curator.checkExists().forPath(nodeName);

		if (stat == null) {
			String result = "";
			if (Strings.isNullOrEmpty(value)) {
				result = curator.create().creatingParentsIfNeeded().forPath(nodeName);
			} else {
				result = curator.create().creatingParentsIfNeeded().forPath(nodeName, value.getBytes(Charsets.UTF_8));
			}
			suc = Objects.equals(nodeName, result);
		}

		return suc;
	}

	/**
	 * update Node
	 * 
	 * @param curator
	 * @param nodeName
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public boolean updateNode(CuratorFramework curator, String nodeName, String value) throws Exception {

		boolean suc = false;

		Stat stat = curator.checkExists().forPath(nodeName);

		if (stat != null) {
			curator.setData().forPath(nodeName, value.getBytes(Charsets.UTF_8));
			suc = true;
		}
		return suc;

	}

	/**
	 * delete Node
	 * 
	 * @param curator
	 * @param nodeName
	 * @throws Exception
	 */
	public void deleteNode(CuratorFramework curator, String nodeName) throws Exception {

		curator.delete().deletingChildrenIfNeeded().forPath(nodeName);
	}

	/**
	 * List all children path 
	 * 
	 * @param curator
	 * @param nodeName
	 * @return
	 * @throws Exception
	 */
	public List<String> listChildren(CuratorFramework curator, String nodeName) throws Exception {
		return curator.getChildren().forPath(nodeName);
	}

	/**
	 * List all children path and value
	 * 
	 * @param curator
	 * @param nodeName
	 * @return
	 * @throws Exception
	 */
	public Map<String, String> listChildrenDetail(CuratorFramework curator, String nodeName) throws Exception {

		Map<String, String> map = Maps.newHashMap();

		List<String> childs = curator.getChildren().forPath(nodeName);
		GetDataBuilder dataBuilder = curator.getData();
		
		if (childs != null) {

			for (String child : childs) {
				String propPath = ZKPaths.makePath(nodeName, child);
				map.put(child, new String(dataBuilder.forPath(propPath), Charsets.UTF_8));
			}
		}

		return map;
	}

}
